<?php

declare(strict_types=1);

namespace App\Nats\Publish;

use Hyperf\Di\Annotation\Inject;
use Hyperf\Nats\Driver\DriverInterface;

class NatsPublish
{
    /**
     * @Inject
     * @var DriverInterface
     */
    protected $nats;

    public function publish($data = [], $subject = 'hyperf.demo')
    {
        $res = $this->nats->publish($subject, $data);
        return $res;
    }

    public function sync($data = [], $subject = 'hyperf.demo')
    {
        /** @var Message $message */
        try{
            $message = $this->nats->requestSync($subject, $data);
            return $message->getBody();
        }
        catch(\Throwable $t){
            file_put_contents(BASE_PATH . '/runtime/log.txt',$t->getMessage().PHP_EOL,FILE_APPEND);
            file_put_contents(BASE_PATH . '/runtime/log.txt',$t->getTrace().PHP_EOL,FILE_APPEND);
        }
        
    }
}